package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.databinding.HistorySearchItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/26.
 */
class HistoryRecordListAdapter(private val list: List<Any>,
                               private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.history_search_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            (holder as ViewHolder).databinding.tvSearchRecord.text = list[position] as String
        }
    }

    inner class ViewHolder(val databinding: HistorySearchItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(adapterPosition, list[adapterPosition])
                }
            }
        }
    }
}